import Role from "./Role";

const {ccclass, property} = cc._decorator;
@ccclass
export default class Hero extends Role {
    /** 
     * 动画分析 
     * 1 正常状态
     * 2 攻击
     * 3 二连击
     * 4 蓄力攻击
    */
    camp = "player";


    
    

    

    //攻击
    attack(target){
        this.hideBar();
        //准备攻击 。 普通攻击 分为三种 2攻击  3二连击 4 蓄力攻击
        this.target = target;
      
        //概率 0~99
        var random = Math.floor(Math.random()*100);
        //0~59 60% 2攻击
        //60~89 30% 3二连击
        //90~99 10% 蓄力攻击
        
        if(random<60){
            //cc.log("普通攻击");
            this.animation.play("2");
        }else if(random>60 && random<90){
            //cc.log("二连击");
            this.animation.play("3");
        }else if(random>90){
            //cc.log("蓄力攻击");
            this.animation.play("4");
        }

        

        

    }

    /**
     * 攻击动画控制
     * @param action move:移动到目录前面 reset回到初始位置 end:攻击结束
     */
    AttackAnim(action:string){
        switch(action){
            case "move":
                this.AttackMove();
            break;
            case "reset":
                this.AttackReset();
            break;
            case "end":
                this.AttackEnd();
            break;
        }
    }

    /** 移动到目标前 */
    private AttackMove(){
        this.animation.pause();
        var tp = this.target.node.position.sub(this.node.position).normalize().mul( 50 );
        var a1 = cc.moveTo(0.5,this.target.node.position.sub(tp));
        var a2 = cc.callFunc(()=>{
            this.target.Beaten();
            this.animation.resume();
        })
        this.node.runAction( cc.sequence(a1,a2) );        
    }
    /** 复位 */
    private AttackReset(){        
        this.animation.pause();
        var a1 = cc.delayTime(0.2);
        var a2 = cc.callFunc(()=>{
            this.animation.resume();            
        })           
        var a3  = cc.jumpTo(0.3, this.originPosition,60,1);
        var a4 = cc.callFunc(()=>{
            cc.log(this.node.position);
        })
        this.node.runAction(cc.sequence(a1,a2,a3,a4));
    }
    /** 攻击结束 */
    private AttackEnd(){     
        this.animation.play("1");
        this.RoleEnd();
    }

    /** 被攻击时 */
    Beaten(){
        var a1 = cc.moveBy(0.5,20,0);
        var a2 = cc.delayTime(0.8);
        var a3 = cc.callFunc(()=>{
            this.SubHpTip(0);
        })
        var a4 = cc.moveBy(0.5,-20,0);
        this.node.runAction(cc.sequence(a1,a2,a3,a4));        
    }
        
}
